import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import '../../../../../components/yt_text_style.dart';
import '../models/service_provider_model.dart';

///展示安装商底部弹窗
class CheckoutWidget extends StatelessWidget {
  final List<ServiceProviderModel> deviceInstallerList;
  final Function()? hiddenCallBack; //点击取消按钮
  final Function()? confirmCallBack; //点击确认按钮
  final Function()? updateCallBack; //选择了安装商需要去update数据的回调

  const CheckoutWidget(this.deviceInstallerList,
      {this.hiddenCallBack,
      this.confirmCallBack,
      this.updateCallBack,
      Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      height: ScreenUtil().screenHeight,
      width: ScreenUtil().screenWidth,
      color: const Color.fromRGBO(0, 0, 0, 0.61),
      child: Column(
        children: [
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {
              if (hiddenCallBack != null) {
                hiddenCallBack!();
              }
            },
            child: Container(
              height: ScreenUtil().screenHeight -
                  (298 + ScreenUtil().bottomBarHeight),
            ),
          ),
          Container(
            color: Colors.white,
            height: 298 + ScreenUtil().bottomBarHeight,
            child: Column(
              children: [
                SizedBox(
                  height: 48,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      Gaps.hGap16,
                      GestureDetector(
                        behavior: HitTestBehavior.opaque,
                        onTap: () {
                          if (hiddenCallBack != null) {
                            hiddenCallBack!();
                          }
                        },
                        child: Text(
                          ResKey.cancel.tr,
                          style: ytTextStyle(fontSize: 16),
                        ),
                      ),
                      const Spacer(),
                      Text(
                        ResKey.selectInstaller.tr,
                        textAlign: TextAlign.center,
                        style: ytTextStyle(
                            fontSize: 16, fontWeight: FontWeight.w500),
                      ),
                      const Spacer(),
                      GestureDetector(
                        onTap: () {
                          if (confirmCallBack != null) {
                            ///先去判断是否选择了安装商,如果安装商为空则不让返回
                            bool isChooseInstaller = false;
                            for (var element in deviceInstallerList) {
                              if (element.isSelected == true) {
                                isChooseInstaller = true;
                                break;
                              }
                            }
                            if (isChooseInstaller) {
                              confirmCallBack!();
                            } else {
                              BrnToast.show(ResKey.installManuTips.tr, context,
                                  duration: BrnDuration.short,
                                  verticalOffset: 100);
                            }
                          }
                        },
                        child: Text(
                          ResKey.sure.tr,
                          style: ytTextStyle(
                              fontSize: 16, color: Colour.primaryColor),
                        ),
                      ),
                      Gaps.hGap16,
                    ],
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(left: 16, right: 16),
                  height: 1,
                  color: Colour.dividerColor,
                ),
                SizedBox(
                  height: 250,
                  child:
                      // SmartRefresher(
                      //   // header: const MaterialClassicHeader(),
                      //   controller: controller.refreshController,
                      //   // header: ListTips.classicHeader,
                      //   // footer: ListTips.generateClassicFooter,
                      //   // onRefresh: () => controller.refreshData(),
                      //   // onLoading: () => controller.loadMoreData(),
                      //   enablePullDown: false,
                      //   enablePullUp: false,
                      //   child:
                      ListView.builder(
                    padding: EdgeInsets.zero,
                    shrinkWrap: true,
                    itemBuilder: (BuildContext context, int index) =>
                        _itemWidget(deviceInstallerList[index]),
                    itemCount: deviceInstallerList.length,
                  ),
                  // )
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  _itemWidget(ServiceProviderModel model) {
    return SizedBox(
      height: 50,
      child: Row(
        children: [
          Gaps.hGap16,
          Text(
            model.orgName.toString(),
            style: ytTextStyle(color: Colour.mainTextColor, fontSize: 16),
          ),
          const Spacer(),
          GestureDetector(
            behavior: HitTestBehavior.opaque,
            onTap: () {},
            child: Container(
              padding: const EdgeInsets.only(right: 16),
              child: IconButton(
                icon: (model.isSelected != null && model.isSelected! == true)
                    ? const Icon(
                        Icons.check_circle,
                        color: Colour.primaryColor,
                      )
                    : const Icon(
                        Icons.circle_outlined,
                        color: Colour.diabledButtoBgColor,
                      ),
                onPressed: () {
                  if (model.isSelected != null && model.isSelected == true) {
                    model.isSelected = false;
                  } else {
                    model.isSelected = true;
                  }

                  if (updateCallBack != null) {
                    updateCallBack!();
                  }
                },
              ),
            ),
          )
        ],
      ),
    );
  }
}
